<?php
	error_reporting(0);
	session_start();
	if(!session_is_registered(myusername)){
		header("location:main_login.php");
	}
	$dbconn = pg_connect ("host=localhost port=5432 dbname=postgres user=postgres password=leliput93");
	if (!$dbconn) {
		echo "An error occured.\n";
		exit;
	}
	$sign=false;
	
	extract($_POST);
	if ($submit) {
		$sign=true;
		$date1=strtotime("$year1-$month1-$day1");
		$date2=strtotime("$year2-$month2-$day2");
		
		if($date1>$date2){
			$err="<label style=color:#FF0000> Input tanggal pada \"Periode Transaksi\" tidak valid.\n
         Periode \"sampai\" tidak boleh lebih dahulu dibandingkan periode \"dari\". </label>";
			$sign=false;
		}
		if (!preg_match("/[^0-9]/",$jumlah)){
			if ($jumlah == 0) {
				$sign=false;
				$err1="<label style=color:#FF0000> Input \"Jumlah Maksimal Produk\" tidak boleh bernilai 0. </label>";
			}	
		}
		else{
			$sign=false;
			$err1="<label style=color:#FF0000> Input \"Jumlah Maksimal Produk\" harus bernilai bilangan bulat. Tidak boleh huruf. </label>";
		}
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<title>Supermarket</title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" href="style.css" type="text/css" />
		<script type="text/javascript" src = "tgl.js"></script>
	</head>

	<body>
	    
		<div id="container" class="clearfix">
			<h1>HEMART B10<em> CABANG <?php echo " $_SESSION[namacabang]";?></em></h1>
			<h2></h2>
		     
			 <div id="content">
				<?php  if ($sign==false) { ?>
					<h3>Daftar Produk Terlaku</h3>
					<br/>
					<b>Pilihlah kriteria untuk menampilkan rekapitulasi transaksi pembelian:</b><br/><br/>
					<form name="dft_transaksi" method = "post" action = "produk_terlaku.php">
						<label><b>Cabang:</b></label><br/>
						<select name = "kode_cabang">
						<?php 
							$result = pg_query($dbconn, "select kode from supermarket.cabang");
							while ($row = pg_fetch_array($result))
									print("<option>".$row[0]."</option>");
						?>
						</select><br/><br/>
							
						<label><b>Periode transaksi :</b></label><br/>
						Dari 
							<SCRIPT>fill_select_dft1(document.dft_transaksi); year_install_dft1(document.dft_transaksi);</script>
						sampai
							<SCRIPT>fill_select_dft2(document.dft_transaksi); year_install_dft2(document.dft_transaksi);</script>
						<?php echo "$err"; ?><br/><br/>
						
						<b>Jumlah maksimal produk: </b><input type = "text" size= '5' name = "jumlah" value = "0"/> 
						<br/>
						<?php echo "$err1"; ?>
						<br/><br/>
						<input type="submit" name="submit" id="button" value="Submit" />
						<input type="reset" name="button" id="button" value="Reset" />
						</p>
						<br/><br/>
					</form> 
				<?php } 
				else {
					echo "<h3>Daftar Produk Terlaku</h3>
						<br/>";
					
					$nama_bulan=array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
					$awal = $year1."-".$month1."-".$day1;
					$akhir = $year2."-".$month2."-".$day2;
				
					$namacabang = $_POST['kode_cabang'];
					
					$kuantitas = $_POST['jumlah'];
					$num = (int)$kuantitas;
					
					$result = pg_query($dbconn, "select p.nama, sum(pp.kuantitas) as jumlah_item, sum (pp.kuantitas*h.harga) as total_penjualan
									from (((supermarket.penjualan pn natural join supermarket.penjualan_produk pp) natural join supermarket.produk p) natural join supermarket.harga h)
									where pn.kode_cabang = '$kode_cabang' and tgl_transaksi >= '$awal' and tgl_transaksi <= '$akhir'
									group by p.kode_produk, p.nama 
									order by jumlah_item desc");
					
					$count = pg_num_rows($result);
					if ($count == 0)
						echo "Tidak ada penjualan pada periode ".$day1." ".$nama_bulan[$month1-1]." ".$year1." sampai ".$day2." ".$nama_bulan[$month2-1]." ".$year2." di cabang $namacabang";
					else {
						echo "Pada periode ".$day1." ".$nama_bulan[$month1-1]." ".$year1." sampai ".$day2." ".$nama_bulan[$month2-1]." ".$year2." ada " . $count . " penjualan<br/><br/>";
						echo "<br/><b>Tabel $num Produk Terlaku</b><br/><br/>";
						echo "<table>
							<th>Nama Produk</th>
							<th>Jumlah Item Terjual</th>
							<th>Total Penjualan</th>";
						
						for ( $counter = 0; ($counter < $num) && ($row = pg_fetch_row( $result ) ); $counter++ )
						{
							// build table to display results
							print( "<tr>" );

							print( "<td>$row[0]</td>" );
							print( "<td>$row[1]</td>" );
							print( "<td align = 'right'>$row[2]</td>" );

							print( "</tr>" );
						} // end for
						
						echo "</table>";
					}
				}
				?>
			</div>
		 
			<ul id="nav">
				<li><a href="login_success.php">Home</a></li>
				<?php 
					
					if ($_SESSION['jabatan'] == 'Admin') 
					echo "<li><a href='form_akun.php'>Buat akun</a></li>";
				?>
				<li><a href="q1.php">List Daftar Cabang</a></li>
				<?php 
					if ($_SESSION['jabatan'] == 'Manajer Operasional') {
						echo "<li><a href='pencatatan1.php'>Pencatatan Transaksi Pembelian</a></li>";
					}
				?>
				<li><a href="daftar_transaksi1.php">Daftar Transaksi Pemasok</a></li>
				<li><a href="rekapitulasi1.php">Rekapitulasi Transaksi Pembelian</a></li>
				<li><a href="produk_terlaku.php" >Daftar Produk Terlaku</a></li>
				<li><a href="B10.php" title="View B10 info">About us</a></li>
				<li><a href="logout.php">Logout</a></li>
			</ul>
		
		</div>
	 
		<address>
			&copy; 2008 All Rights Reserved. Designed by <a href="http://www.caddoo.net" title="Portfolio of Matthew Caddoo">Matthew Caddoo</a> for <a href="http://www.zymic.com" title="Zymic Free Templates">Zymic</a> <a href="http://www.zymic.com/free-templates">Free Templates</a>. <a href="http://www.zymic.com/free-web-hosting/">Free Web Hosting</a>.
		</address>

	</body>
</html>
